package com.consult.controller;

import com.consult.config.dto.AjaxResult;
import com.consult.config.dto.LoginDto;
import com.consult.config.dto.RegisterDto;
import com.consult.entity.Admin;
import com.consult.entity.Consultant;
import com.consult.entity.Student;
import com.consult.entity.Visitor;
import com.consult.enums.UserTypeEnum;
import com.consult.service.IAdminService;
import com.consult.service.IConsultantService;
import com.consult.service.IStudentService;
import com.consult.service.IVisitorService;
import com.consult.utils.TokenUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author  
 * @version 1.0
 * @date 2021/6/25 0025 14:42
 */
@RestController
public class LoginController {

    @Resource
    private IAdminService adminService;
    @Resource
    private IVisitorService visitorService;
    @Resource
    private IStudentService studentService;
    @Resource
    private IConsultantService consultantService;

    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginDto login) {
        LoginDto user;
        if (UserTypeEnum.ADMIN.getIndex() == login.getUserType()) {
            user = adminService.getByUserName(login.getName());
        } else if (UserTypeEnum.CONSULTANT.getIndex() == login.getUserType()) {
            user = consultantService.getByUserName(login.getName());
        } else if (UserTypeEnum.VISITOR.getIndex() == login.getUserType()) {
            user = visitorService.getByUserName(login.getName());
        } else if (UserTypeEnum.STUDENT.getIndex() == login.getUserType()) {
            user = studentService.getByUserName(login.getName());
        } else {
            return AjaxResult.error("请选择登录用户类型");
        }
        if (null == user) {
            return AjaxResult.error("登录失败,用户不存在");
        }
        if (!login.getPassword().equals(user.getPassword())) {
            return AjaxResult.error("登录失败,密码错误");
        }
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = TokenUtil.sign(user, 1);
        ajax.put("token", token);
        ajax.put("userType", login.getUserType());
        return ajax;
    }

    @PostMapping("/register")
    public AjaxResult register(@RequestBody RegisterDto registerDto) {
        if (UserTypeEnum.ADMIN.getIndex() == registerDto.getUserType()) {
            Admin admin = new Admin();
            admin.setUsername(registerDto.getUsername());
            admin.setPassword(registerDto.getPassword());
            admin.setPhone(registerDto.getPhone());
            adminService.save(admin);
        } else if (UserTypeEnum.CONSULTANT.getIndex() == registerDto.getUserType()) {
            Consultant consultant = new Consultant();
            consultant.setUsername(registerDto.getUsername());
            consultant.setPassword(registerDto.getPassword());
            consultant.setPhone(registerDto.getPhone());
            consultantService.save(consultant);
        } else if (UserTypeEnum.VISITOR.getIndex() == registerDto.getUserType()) {
            Visitor visitor = new Visitor();
            visitor.setUsername(registerDto.getUsername());
            visitor.setPassword(registerDto.getPassword());
            visitor.setPhone(registerDto.getPhone());
            visitorService.save(visitor);
        } else if (UserTypeEnum.STUDENT.getIndex() == registerDto.getUserType()) {
            Student student = new Student();
            student.setUsername(registerDto.getUsername());
            student.setPassword(registerDto.getPassword());
            student.setPhone(registerDto.getPhone());
            studentService.save(student);
        } else {
            return AjaxResult.error("请选择登录用户类型");
        }
        return AjaxResult.success();
    }

}
